package com.cat.dataStructure;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/longest-consecutive-sequence/?envType=study-plan-v2&envId=top-100-liked
 * @create 2025/9/25 20:42
 * @since JDK17
 */

public class Solution78 {
    public int longestConsecutive(int[] nums) {
        int ans = 1, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }

        for (int num : set) {
            if (!set.contains(num - 1)) {   // 为了保证是第一个
                int cur = num, cnt = 1;
                while (set.contains(cur + 1)) {
                    cur++;
                    cnt++;
                }
                ans = Math.max(ans, cnt);
            }
        }

        return ans;
    }
}
